package dao;

import util.DBHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OrderDao {

    //查询所有的用户数据
    public long queryTotal(String receivername,String orderid,String status,String createtime){
        String sql = "select o.*,uname,receivername from shopping_order o   INNER JOIN receshopping r ON o.shoppingid=r.shoppingid inner join shopping_user u on u.uid = r.uid where 1=1 ";
        ArrayList<Object> params = new ArrayList<>();
        if(receivername !=null && receivername.trim().isEmpty() == false){
            sql += " and receivername like ? ";
            params.add("%" + receivername + "%");
        }

        if(orderid != null && orderid.trim().isEmpty() == false){
            sql += " and o.orderid = ?";
            params.add(orderid);
        }

        if(status != null && status.trim().isEmpty() == false){
            sql += " and o.status = ?";
            params.add(status);
        }

        if(createtime != null && createtime.trim().isEmpty() == false){
            sql += " and createtime between ? and ?";
            params.add(createtime + " 00:00:00");
            params.add(createtime + " 23:59:59");
        }
        return DBHelper.count(sql,params.toArray());
    }

    //查询收货人
    public List<Map<String,Object>> queryReceivename(String name,String orderid,String status,String createtime,int begin, int size){
        String sql = "select o.*,uname,receivername from shopping_order o   INNER JOIN receshopping r ON o.shoppingid=r.shoppingid inner join shopping_user u on u.uid = r.uid where 1=1";
        ArrayList<Object> params = new ArrayList<>();
        if(name != null && name.trim().isEmpty() == false){
            sql += " and receivername like ?";
            params.add("%" + name + "%");
        }
        if(orderid != null && orderid.trim().isEmpty() == false){
            sql += " and o.orderid = ?";
            params.add(orderid);
        }

        if(status != null && status.trim().isEmpty() == false){
            sql += " and o.status = ?";
            params.add(status);
        }

        if(createtime != null && createtime.trim().isEmpty() == false){
            sql += " and createtime between ? and ?";

            params.add(createtime + " 00:00:00");
            params.add(createtime + " 23:59:59");

        }
        sql += " limit "+ begin + "," + size;
        return DBHelper.selectListMap(sql,params.toArray());
    }
}
